package com.raycloud.query;

                    import java.util.*;
/**
 *
 * @author lvduo (lvduo940613@gmail.com)
 */
public class FrontProjectQuery extends BaseQuery {

	/**==============================批量查询、更新、删除时的Where条件设置==================================**/
	/** project_name **/
    private String projectName;
	public String getProjectName () {
    	return projectName;
   	}
    public FrontProjectQuery setProjectName(String projectName) {
    	this.projectName = projectName;
    	return this;
    }
	/** git_path **/
    private String gitPath;
	public String getGitPath () {
    	return gitPath;
   	}
    public FrontProjectQuery setGitPath(String gitPath) {
    	this.gitPath = gitPath;
    	return this;
    }
	/** local_address **/
    private String localAddress;
	public String getLocalAddress () {
    	return localAddress;
   	}
    public FrontProjectQuery setLocalAddress(String localAddress) {
    	this.localAddress = localAddress;
    	return this;
    }
	/** branch **/
    private String branch;
	public String getBranch () {
    	return branch;
   	}
    public FrontProjectQuery setBranch(String branch) {
    	this.branch = branch;
    	return this;
    }
	/** owner **/
    private String owner;
	public String getOwner () {
    	return owner;
   	}
    public FrontProjectQuery setOwner(String owner) {
    	this.owner = owner;
    	return this;
    }
	/**==============================批量查询时的Order条件顺序设置==================================**/
	public class OrderField{
		public OrderField(String fieldName, String order) {
			super();
			this.fieldName = fieldName;
			this.order = order;
		}
		private String fieldName;
		private String order;
		public String getFieldName() {
			return fieldName;
		}
		public OrderField setFieldName(String fieldName) {
			this.fieldName = fieldName;
			return this;
		}
		public String getOrder() {
			return order;
		}
		public OrderField setOrder(String order) {
			this.order = order;
			return this;
		}
	}

	/**==============================批量查询时的Order条件顺序设置==================================**/
	/**排序列表字段**/
	private List<OrderField> orderFields = new ArrayList<OrderField>();
	/**
	 * 设置排序按属性：project_name
	 * @param isAsc 是否升序，否则为降序
	 */	
	public FrontProjectQuery orderbyProjectName(boolean isAsc){
		orderFields.add(new OrderField("project_name",isAsc?"ASC":"DESC"));
		return this;
	}
	/**
	 * 设置排序按属性：git_path
	 * @param isAsc 是否升序，否则为降序
	 */	
	public FrontProjectQuery orderbyGitPath(boolean isAsc){
		orderFields.add(new OrderField("git_path",isAsc?"ASC":"DESC"));
		return this;
	}
	/**
	 * 设置排序按属性：local_address
	 * @param isAsc 是否升序，否则为降序
	 */	
	public FrontProjectQuery orderbyLocalAddress(boolean isAsc){
		orderFields.add(new OrderField("local_address",isAsc?"ASC":"DESC"));
		return this;
	}
	/**
	 * 设置排序按属性：branch
	 * @param isAsc 是否升序，否则为降序
	 */	
	public FrontProjectQuery orderbyBranch(boolean isAsc){
		orderFields.add(new OrderField("branch",isAsc?"ASC":"DESC"));
		return this;
	}
	/**
	 * 设置排序按属性：owner
	 * @param isAsc 是否升序，否则为降序
	 */	
	public FrontProjectQuery orderbyOwner(boolean isAsc){
		orderFields.add(new OrderField("owner",isAsc?"ASC":"DESC"));
		return this;
	}
    private String fields;
    /**
     * 提供自定义字段使用
     */
    private static Map<String,String> fieldMap;

    private static Map<String,String> getFieldSet() {
        if (fieldMap == null){
            fieldMap =new HashMap<String,String>();
                    fieldMap.put("project_name", "projectName");
                    fieldMap.put("git_path", "gitPath");
                    fieldMap.put("local_address", "localAddress");
                    fieldMap.put("branch", "branch");
                    fieldMap.put("owner", "owner");
                }
        return fieldMap;
    }

    public String getFields(){
        return this.fields;
    }
    public FrontProjectQuery  setFields(String fields){
        String[] array = fields.split(",");
        StringBuffer buffer = new StringBuffer();
        for (String field : array){
            if(getFieldSet().containsKey(field)){
                buffer.append(field).append(" as ").append(getFieldSet().get(field)).append(" ,");
            }
            if(getFieldSet().containsKey("`"+field+"`")){
                buffer.append("`"+field+"`").append(" as ").append(getFieldSet().get(field)).append(" ,");
            }
        }
        if (buffer.length() != 0){
            this.fields = buffer.substring(0, buffer.length() - 1);
        }else{
            this.fields = " 1 ";//没有一个参数可能会报错
        }
        return this;
    }
}
